
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Namespace _2bMatesSite.BO
	''' <summary>
	''' Pictures management.
	''' </summary>
	Public Class Pictures
		Public Shared Function GetPictureSrcFullURL(ByVal pic As GroupPicture, ByVal param As String, ByVal path As String) As String
			Dim url As String = Constants.IMAGE_PAGE

			If pic <> Nothing AndAlso pic.ID <> -1 Then
				url = GetPictureSrcFullURL(pic.ID, pic.GroupID, Constants.IMAGE_IS_GROUP, param, path)
			End If

			Return url
		End Function

		Public Shared Function GetPictureSrcFullURL(ByVal pic As UserPicture, ByVal param As String, ByVal path As String) As String
			Dim url As String = Constants.IMAGE_PAGE

			If pic <> Nothing AndAlso pic.ID <> -1 Then
				url = GetPictureSrcFullURL(pic.ID, pic.UserID, Constants.IMAGE_IS_USER, param, path)
			End If

			Return url
		End Function

		' objID is the ID of the object that refers the picture (e.g. user, group)
		Public Shared Function GetPictureSrcFullURL(ByVal picID As Integer, ByVal objID As Integer, ByVal objType As String, ByVal param As String, ByVal path As String) As String
			Dim url As String = Constants.IMAGE_PAGE

			url += "?" + Constants.IMAGE_ID + "=" + picID.ToString()
			url += "&" + Constants.IMAGE_OBJID + "=" + objID.ToString()
			url += "&" + objType + "=true"

			If param <> Nothing AndAlso param.Length > 0 Then
				url += "&" + param + "=true"
			End If

			url = path + url

			Return url
		End Function

		Public Shared Function GetPictureSrcFullURL(ByVal pic As GroupPicture, ByVal thumbNail As Boolean, ByVal path As String) As String
			If thumbNail = True Then
				Return GetPictureSrcFullURL(pic, Constants.IMAGE_IS_THUMB, path)
			Else
				Return GetPictureSrcFullURL(pic, "", path)
			End If
		End Function

		Public Shared Function GetPictureLargeThumbnailSrcFullURL(ByVal pic As GroupPicture, ByVal path As String) As String
			Return GetPictureSrcFullURL(pic, Constants.IMAGE_IS_LARGE_THUMB, path)
		End Function

		Public Shared Function GetPictureSrcFullURL(ByVal pic As UserPicture, ByVal thumbNail As Boolean, ByVal path As String) As String
			If thumbNail = True Then
				Return GetPictureSrcFullURL(pic, Constants.IMAGE_IS_THUMB, path)
			Else
				Return GetPictureSrcFullURL(pic, "", path)
			End If
		End Function

		Public Shared Function GetPictureLargeThumbnailSrcFullURL(ByVal pic As UserPicture, ByVal path As String) As String
			Return GetPictureSrcFullURL(pic, Constants.IMAGE_IS_LARGE_THUMB, path)
		End Function


		Public Shared Function GetImageFormatMIMEType(ByVal imf As ImageFormat) As String
			Dim mime As String = "image/bmp"

			If imf.Equals(ImageFormat.Bmp) Then
				mime = "image/bmp"
ElseIf imf.Equals(ImageFormat.Gif) Then
				mime = "image/gif"
ElseIf imf.Equals(ImageFormat.Jpeg) Then
				mime = "image/jpeg"
ElseIf imf.Equals(ImageFormat.Png) Then
				mime = "image/png"
			Else
				Throw New Exception("Unsupported image format : " + imf.ToString())
			End If

			Return mime
		End Function

		Public Shared Function ResizeBitmap(ByVal src As Bitmap, ByVal newSize As Size) As Bitmap
			Return ResizeBitmap(src, newSize.Width, newSize.Height)
		End Function

		Public Shared Function ResizeBitmap(ByVal src As Bitmap, ByVal newWidth As Integer, ByVal newHeight As Integer) As Bitmap
			Dim imf As ImageFormat = src.RawFormat

			Dim newImage As New Bitmap(src, newWidth, newHeight)
			Dim gr As Graphics = Graphics.FromImage(newImage)

			gr.DrawImage(src, New Rectangle(0, 0, newWidth, newHeight), 0, 0, src.Width, src.Height, _
				GraphicsUnit.Pixel)
			gr.Dispose()

			Dim ms As New MemoryStream()
			newImage.Save(ms, imf)

			Dim lastImage As New Bitmap(ms)

			Return lastImage
		End Function


		' Calculates a new image size, that would fit
		' in the max allowed, keeping the aspect ratio
		Public Shared Function FitSize(ByVal curSize As Size, ByVal maxSize As Size, ByVal isAddition As Boolean) As Size
			Dim rW As Double = 1
			Dim rH As Double = 1
			Dim scale As Double = 1

			' Calculate scale ratio to fit with and height
			' keeping it to 1 if no scaling is needed
			If curSize.Width > maxSize.Width Then
				rW = DirectCast(curSize.Width, Double) / DirectCast(maxSize.Width, Double)
			End If
			If curSize.Width < maxSize.Width AndAlso isAddition = False Then
				rW = DirectCast(maxSize.Width, Double) / DirectCast(curSize.Width, Double)
			End If
			If curSize.Height > maxSize.Height Then
				rH = DirectCast(curSize.Height, Double) / DirectCast(maxSize.Height, Double)
			End If

			' Take the max
			scale = Math.Max(rW, rH)

			' Scale the image only if needed
			If scale > 1 Then
				If curSize.Width < maxSize.Width Then
					curSize.Width = DirectCast((curSize.Width * scale), Integer)
					curSize.Height = DirectCast((curSize.Height * scale), Integer)
				Else
					curSize.Width = DirectCast((curSize.Width / scale), Integer)
					curSize.Height = DirectCast((curSize.Height / scale), Integer)
				End If
			End If

			Return curSize
		End Function
	End Class
End Namespace






